define('holeApp', [], function() {
    var scene,
        camera,
        controls,
        shadowLight,
        backLight,
        light,
        renderer,
        container,
        raycaster;

    //SCREEN VARIABLES
    var HEIGHT,
        WIDTH,
        windowHalfX,
        windowHalfY,
        mousePos = {
            x: 0,
            y: 0
        };

    var init = function(param) {
        var container = param.container;
        scene = new THREE.Scene();
        raycaster = new THREE.Raycaster();

        HEIGHT = window.innerHeight;
        WIDTH = window.innerWidth;

        var aspectRatio = WIDTH / HEIGHT;
        var fieldOfView = 60;
        var nearPlane = 1;
        var farPlane = 2000;

        camera = new THREE.PerspectiveCamera(
            fieldOfView,
            aspectRatio,
            nearPlane,
            farPlane);
        // camera.position.x = -500;
        camera.position.z = 1000;
        camera.position.y = 100;

        camera.lookAt(new THREE.Vector3(0, 0, 0));

        renderer = new THREE.WebGLRenderer({
            alpha: true,
            antialias: true,
            clearColor: 0x000000
        });

        renderer.setClearColor(0x000000, 1);
        renderer.setSize(WIDTH, HEIGHT);
        renderer.shadowMap.enabled = true;

        container = container;
        container.appendChild(renderer.domElement);

        windowHalfX = WIDTH / 2;
        windowHalfY = HEIGHT / 2;

        window.addEventListener('resize', onWindowResize, false);
        document.addEventListener('mouseup', handleMouseUp, false);
        document.addEventListener('touchend', handleTouchEnd, false);

        //*
        controls = new THREE.OrbitControls(camera, renderer.domElement);
        controls.minPolarAngle = -Math.PI / 2;
        controls.maxPolarAngle = Math.PI / 2 + .1;
        controls.noZoom = false;
        controls.noPan = false;

        createLights();

        createOrigin();
        build();
    };

    function onWindowResize() {
        HEIGHT = window.innerHeight;
        WIDTH = window.innerWidth;
        windowHalfX = WIDTH / 2;
        windowHalfY = HEIGHT / 2;
        renderer.setSize(WIDTH, HEIGHT);
        camera.aspect = WIDTH / HEIGHT;
        camera.updateProjectionMatrix();
    };

    function handleMouseUp() {
        // body...
    };

    function handleTouchEnd() {
        // body...
    };



    function createOrigin() {
        var axis = new THREE.AxisHelper(1000);
        scene.add(axis);
    };

    function createLights() {
        light = new THREE.HemisphereLight(0xffffff, 0xb3858c, .65);

        shadowLight = new THREE.DirectionalLight(0xffe79d, .7);
        shadowLight.position.set(80, 120, 50);
        shadowLight.castShadow = true;
        shadowLight.shadow.mapSize.width = 2048;
        shadowLight.shadow.mapSize.height = 2048;

        backLight = new THREE.DirectionalLight(0xffffff, .4);
        backLight.position.set(200, 100, 100);
        backLight.castShadow = true;

        scene.add(backLight);
        scene.add(light);
        scene.add(shadowLight);
    };

    var faceIndices = ['a', 'b', 'c'];
    var build = function() {
        var segmentColors = ['red', 'orange', 'green', 'pink', ];
        var waterGeo = new THREE.BoxGeometry(1000, 100, 1000, 22, 1, 22);
        for (var i = 0; i < waterGeo.vertices.length; i++) {
            var vertex = waterGeo.vertices[i];
            if (vertex.z > 0)
                vertex.z += Math.random() * 2 - 1;

            if (vertex.x == 0 ) continue;
            vertex.x += Math.random() * 5 - 2.5;
            vertex.y += Math.random() * 5 - 2.5;

            vertex.wave = Math.random() * 100;
        }

        waterGeo.computeFaceNormals();
        waterGeo.computeVertexNormals();

        var seg = 250;
        var t = 25;
        var color1 = new THREE.Color(segmentColors[0]);
        var color2 = new THREE.Color(segmentColors[1]);
        var color3 = new THREE.Color(segmentColors[2]);
        var color4 = new THREE.Color(segmentColors[3]);
        for (var i = 0; i < waterGeo.faces.length; i++) {
            var face = waterGeo.faces[i];
            for (var j = 0; j < 3; j++) {
                var vertexIndex = face[faceIndices[j]];
                var point = waterGeo.vertices[vertexIndex];
                // if (point.x > seg + t) {
                //     color = new THREE.Color(color1);
                    
                //     face.vertexColors[j] = color;
                // } 
                // else if(point.x <= seg +t && point.x >= seg - t)
                // {   
                //     // color = new THREE.Color(color1);
                    
                //     // if (Math.abs(point.x - seg)  < 50) {
                //     //     color.setRGB((color1.r + color2.r) / 2,
                //     //         (color1.g + color2.g) / 2,
                //     //         (color1.b + color2.b) / 2);
                //     // }
                //     // face.vertexColors[j] = color;
                // }
                // else if (point.x > 0 + t && point.x < seg - t) {
                //     color = new THREE.Color(color2);
                //     face.vertexColors[j] = color;
                // } 
                // else if (point.x <= 0+t && point.x >= 0 -t)
                // {
                //     // color = new THREE.Color(segmentColors[0]);
                    
                 
                //     // face.vertexColors[j] = color;
                // }
                // else if (point.x > -1 * seg + t  && point.x <  0 -t) {
                //     color = new THREE.Color(color3);
                //     face.vertexColors[j] = color;
                // }
                // else if (point.x <= -1 * seg + t && point.x >= -1 * seg - t)
                // {
                //     // color = new THREE.Color(segmentColors[0]);
                    
                    
                //     // face.vertexColors[j] = color;
                // } else if (point.x < -1 * seg - t) {
                //     color = new THREE.Color(color4);
                //     face.vertexColors[j] = color;
                // }
            };
        };

        var floor = new THREE.Mesh(waterGeo, new THREE.MeshBasicMaterial({
            color: 0x6092c1,
            transparent: true,
            shading: THREE.FlatShading,
            side: THREE.DoubleSide,
            vertexColors: THREE.VertexColors,
            opacity: 0.5,
        }));
        scene.add(floor);

        var secondFloor = new THREE.Mesh(waterGeo, new THREE.MeshBasicMaterial({
            color: 0xcceeff,
            transparent: true,
            shading: THREE.FlatShading,
            side: THREE.DoubleSide,
            opacity: 0.5
        }));
        secondFloor.position.y = -100;

        scene.add(floor);
        scene.add(secondFloor);
        loop();
    };

    var mouse = new THREE.Vector2(), INTERSECTED;

    function render() {
        if (controls) controls.update();

        // if (cube) {
        //   // cube.boxMesh.rotation.y+=.01;
        // }
        renderer.render(scene, camera);

        raycaster.setFromCamera( mouse, camera );
        var intersects = raycaster.intersectObjects( scene.children );

        if ( intersects.length > 0 ) {

        //         if ( INTERSECTED != intersects[ 0 ].object ) {

        //             // if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );

        //             INTERSECTED = intersects[ 0 ].object;
                    
        //             // INTERSECTED.material.emissive.setHex( 0xff0000 );
        //         if(INTERSECTED && INTERSECTED.material.emissive){
        //             INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
        //             INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex);
        //         }
        //         else{
        //             INTERSECTED.material.color.setHex(INTERSECTED.currentHex);
        //         }
        //         }

            } else {

                if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );

                INTERSECTED = null;

            }
    }

    function loop() {
        render();
        requestAnimationFrame(loop);
        renderer.render(scene, camera);
    }
    return {
        init: init,
    };
});